/**
 * Description placeholder
 *
 * @type {*}
 */
const errorCode = Object.freeze({
  /**
   * token验证失败
   */
  'TOKEN': 401,
  /**
   * 权限不足
   */
  'NOT_ALLOW': 403,
  /**
   * 参数验证失败
   */
  'PARAMS_VALIDATE': 501,
  /** 
   * 未知错误
   */
  'UNKNOWN': 500,
});
function throwError(message, code = 500) {
  const err = new Error(message);
  err.code = code;
  throw err;
}
function throwValidateError(err) {
  throw { message: '参数校验失败:' + err.message, code: errorCode.PARAMS_VALIDATE };
}
async function errorHandler(ctx, next) {
  ctx.errorCode = errorCode;
  ctx.throwError = throwError;
  ctx.throwValidateError = throwValidateError;
  return await next();
}
module.exports = {
  errorHandler
};